package www.openteam.com.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import butterknife.ButterKnife;
import okhttp3.internal.Util;
import www.openteam.com.OTApplication;
import www.openteam.com.mvp.IView;

/**
 * Created by kylin on 2016/12/19.
 */

public abstract class BaseActivity<P extends BasePresenter>
        extends AppCompatActivity implements IView, View.OnClickListener {

    protected View view;

    protected P mPresenter;
    public ProgressDialog mProgressDialog;

    public SharedPreferences mySetting = OTApplication.getmContext().getSharedPreferences("mySetting", Context.MODE_PRIVATE);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getView());
        mPresenter = loadPresenter();
        initCommonData();
        initView();
        initData();
        initListener();
    }

    protected abstract P loadPresenter();


    private void initCommonData() {
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
    }


    protected abstract void initData();

    protected abstract void initListener();

    protected abstract void initView();

    protected abstract int getLayoutId();

    protected abstract void otherViewClick(View view);

    /**
     * @return 显示的内容
     */
    public View getView() {
        view = View.inflate(this, getLayoutId(), null);
        return view;
    }

    /**
     * 点击的事件的统一的处理
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            default:
                otherViewClick(view);
                break;
        }

    }

    /**
     * @param str 显示一个内容为str的toast
     */
    public void toast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    /**
     * @param contentId 显示一个内容为contentId指定的toast
     */
    public void toast(int contentId) {
        Toast.makeText(this, contentId, Toast.LENGTH_SHORT).show();
    }


    /**
     * 创建ProgressDialog
     *
     * @param content 要显示的字
     */
    public void createProgressDialog(String content) {


        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setMessage(content);
        mProgressDialog.show();


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        ButterKnife.unbind(this);//解绑
    }


}


